package day1105.first;

import day1105.common.MyUtil;
import day1105.common.Student;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import static day1105.common.MyUtil.printLine;

/**
 * @author Mr.ma
 * @since 2022/11/5 14:38
 * map():对流中的每个元素进行操作后返回新的元素，需要一个函数型函数式接口
 **/
public class StreamMapTest {
    public static void main(String[] args) {
        List<Student> studentList = MyUtil.createStudentList();
        studentList.stream().forEach(System.out::println);
        printLine("将Student对象转换为名字");
        List<String> collect = studentList.stream().map(Student::getName).collect(Collectors.toList());
        System.out.println("collect = " + collect);
        printLine("给学生加一岁");
        Optional<Student> first = studentList.stream()
                .map(x -> { x.setAge(x.getAge() + 1);return x; })
                .findFirst();
        System.out.println("first = " + first.get());
        printLine("拆分字符串数组中的单词然后去重");
        String[] arr = new String[]{"hello","world"};
        List<String[]> mapTest = Arrays.stream(arr).map(x -> x.split("")).distinct().collect(Collectors.toList());
        System.out.println("使用map：" + mapTest);
        Arrays.stream(mapTest.get(0)).forEach(System.out::print);
        List<String> flatMapTest = Arrays.stream(arr).map(x -> x.split("")).flatMap(Arrays::stream).distinct().collect(Collectors.toList());
        System.out.println("使用flatMap：" + flatMapTest);
    }
}
